package com.dating.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;

import com.dating.libs.web.services.datingservice.DatingService;
import com.dating.libs.web.services.datingservice.UserProperties;
import com.dating.pref.Constant;
import com.dating.view.DtEditText;

public class EditInfo extends Activity{
	Handler mHandler;
	LinearLayout lyContent;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.edit_info);
		
		mHandler=new Handler();
		
		lyContent=(LinearLayout)findViewById(R.id.layout_edit_info);
		
		updateUI();
	}
	private void updateUI(){
		if(Dating.ME.mMeProperties.size()>0)
			for(UserProperties _u : Dating.ME.mMeProperties){
				DtEditText _t=new DtEditText(this);
				_t.setProperties(_u.getName(""));
				_t.setText(_u.getValue(""));
				lyContent.addView(_t);
			}
	}
	public void btnClicked(View v){
		switch (v.getId()) {
		case R.id.btn_ok:
			Dating.ME.mMeProperties=new ArrayList<UserProperties>();
			
			// update mMeProperties
			for(int i=0;i<lyContent.getChildCount();++i){
				DtEditText _v=(DtEditText)lyContent.getChildAt(i);
				String _properties=_v.getProperties();
				String _value=_v.getText();
				UserProperties _u=new UserProperties();
				_u.setName(_properties);
				_u.setValue(_value);
				Dating.ME.mMeProperties.add(_u);
			}
			// thread update to server
			new Thread(new Runnable() {
				
				@SuppressWarnings("unchecked")
				@Override
				public void run() {
					mHandler.post(new Runnable() {
						
						@Override
						public void run() {
							showDialog(Constant.DIALOG_EDIT);
						}
					});
					int success=0;
					int failed=0;
					try{
					ArrayList<UserProperties> _list=(ArrayList<UserProperties>)Dating.ME.mMeProperties.clone();
					DatingService dt=new DatingService();
					Log.e("update Properties", "begin");
					for(UserProperties _i:_list){
						try {
							boolean result=dt.updateUserProperties(_i.getName(""), _i.getValue(""));
							if(result){
								success++;
								Log.e("update Properties:"+_i.getName(""), _i.getValue(""));
							}else{
								failed++;
								Log.e("update Properties:"+_i.getName(""), "false");
							}
						} catch (Exception e) {
							Log.e("update Properties:"+_i.getName(""), "failed");
							e.printStackTrace();
							continue;
						}
						Log.e("update Properties", "complete");
					}
					}finally{
						mHandler.post(new Runnable() {
							
							@Override
							public void run() {
								dismissDialog(Constant.DIALOG_EDIT);
								EditInfo.this.finish();
							}
						});
					}
					
					
				}
			}).start();
			break;
		case R.id.btn_cancel:
			this.finish();
			break;

		default:
			break;
		}
	}
	@SuppressWarnings("unused")
	private void addProperties(String name,String value){
		
	}
	// SUPER METHOD
	boolean cancel = false;
	@Override
	@Deprecated
	protected Dialog onCreateDialog(int id) {
		Dialog d = new Dialog(this);
		d.setCancelable(true);
		// d.setCanceledOnTouchOutside(true);
		d.requestWindowFeature(Window.FEATURE_NO_TITLE);
		d.setContentView(R.layout.loading);
		switch (id) {
		case Constant.DIALOG_LOADING:
			d.setOnCancelListener(new DialogInterface.OnCancelListener() {
				@Override
				public void onCancel(DialogInterface dialog) {
					cancel = true;
				}
			});
			// d.setTitle("Loging in...");
			cancel = false;
			d.show();
			break;
		case Constant.DIALOG_EDIT:
			d.setOnCancelListener(new DialogInterface.OnCancelListener() {
				@Override
				public void onCancel(DialogInterface dialog) {
					cancel = true;
				}
			});
			 d.setTitle("Updating...");
			 
			cancel = false;
			d.show();
			break;
		default:
			break;
		}
		return d;
	}
}
